"""Parse content from http://www.whoscored.com
"""

__author__ = 'akshaylal@gmail.com'

import argparse
import logging

import http_operations
import player_data_parser


def GetPlayerData(uri_list):
  """
  """
  player_stats = {}

  for uri in uri_list:
    http_obj = http_operations.HTTPOperations(uri)
    html_data = http_obj.GetPageContent()
    if not html_data:
      return False
    http_obj.Cleanup()

    data_parser = player_data_parser.PlayerDataParser(html_data)
    if not data_parser.ParsePlayerData():
      return False

    # After parsing the data we expect 36 entries in the map.
    if len(data_parser.player_stats_map) != 36:
      logging.error('Failed to parse player stats correctly. Num players '
                    'parsed: %d != 36', len(data_parser.player_stats_map))
      return False


    # Update the values for the player stats, but first ensure that we don't
    # have duplicate keys (player ids).
    for key, value in data_parser.player_stats_map.iteritems():
      if key in player_stats:
        logging.error('Duplicate key: %s found in player_stats map: %s',
                      key, player_stats)
        return False

      player_stats[key] = value

  return player_stats

def SetupLogger(verbosity):
  """
  """
  if verbosity:
    logging.getLogger('').setLevel(logging.DEBUG)
  else:
    logging.getLogger('').setLevel(logging.INFO)


def SetupCommandLineFlags():
  """
  """
  parser = argparse.ArgumentParser(
      description='Parses html content from a specifed uri & returns the player'
                  ' statistics')
  parser.add_argument('-u', '--uri_list', dest='uri_list', required=True,
                      type=str, action='append', help='The list of URI path.')
  parser.add_argument('-v', '--verbose', dest='verbosity', action='store_true',
                      default=False, help='Logging verbosity.')

  return parser

def main():
  parser = SetupCommandLineFlags()
  args = parser.parse_args()

  SetupLogger(args.verbosity)

  player_stats = GetPlayerData(args.uri_list)
  if not player_stats:
    return False

  for key, value in player_stats.iteritems():
    logging.info('%d : %s', key, value['name'])

  return True

if __name__ == '__main__':
  main()
